package org.common4j2ee.stock.web.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.common4j2ee.stock.entity.user.Resources;
import org.common4j2ee.stock.dao.utils.Page;
import org.common4j2ee.stock.dao.utils.PropertyFilter;
import org.common4j2ee.stock.service.user.ResourcesManager;
import org.common4j2ee.stock.service.utils.ServiceException;
import org.common4j2ee.stock.web.utils.CrudActionSupport;
import org.common4j2ee.stock.web.utils.PropertyFilterWeb;
import org.common4j2ee.stock.web.utils.Struts2Utils;
import org.springframework.beans.factory.annotation.Autowired;


@Namespace("/user")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "resources!list.action", type = "redirect") })
public class ResourcesAction extends CrudActionSupport<Resources> {

	private Resources resources;

	private Long id;

	private Page<Resources> page = new Page<Resources>(10);// 每页5条记录

	@Autowired
	ResourcesManager resourcesManager;

	private static final long serialVersionUID = 633445360169800367L;

	// -- CRUD Action 函数 --//
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilterWeb
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = resourcesManager.searchResources(page, filters);
		return SUCCESS;
	}

	public List<Resources> getAllResourceList() {

		return resourcesManager.getAllResourceList();
	}

	@Override
	public String input() throws Exception {

		return INPUT;
	}

	@Override
	public String save() throws Exception {
		resourcesManager.saveAuthority(resources);
		 addActionMessage("保存资源成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			resourcesManager.deleteResources(id);
			 addActionMessage("删除资源成功");
		} catch (ServiceException e) {
			 logger.error(e.getMessage(), e);
			 addActionMessage("删除资源失败");
		}
		return RELOAD;
	}

	public String checkResourceName() {
			HttpServletRequest request = ServletActionContext.getRequest();
			String newLoginName = request.getParameter("name");
			String oldLoginName = request.getParameter("oldRoleName");
			if (resourcesManager.isRoleNameUnique(newLoginName, oldLoginName)) {
				Struts2Utils.renderText("true");
			} else {
				Struts2Utils.renderText("false");
			}
			 //因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id == null) {
			resources = new Resources();
		} else {
			resources = resourcesManager.getAuthority(id);
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Resources getResources() {
		return resources;
	}

	public void setResources(Resources resources) {
		this.resources = resources;
	}

	public Page<Resources> getPage() {
		return page;
	}

	public void setPage(Page<Resources> page) {
		this.page = page;
	}

	public ResourcesManager getResourcesManager() {
		return resourcesManager;
	}

	public void setResourcesManager(ResourcesManager resourcesManager) {
		this.resourcesManager = resourcesManager;
	}

	@Override
	public Resources getModel() {
		return resources;
	}

}
